package com.jjwxc.jwjskandriod.widget;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.Switch;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;

import com.jjwxc.jwjskandriod.R;
import com.jjwxc.jwjskandriod.activity.BookDetailsActivity;
import com.jjwxc.jwjskandriod.activity.WebActivity;
import com.jjwxc.jwjskandriod.config.Url;
import com.jjwxc.jwjskandriod.framework.base.FFApplication;
import com.jjwxc.jwjskandriod.framework.http.FFNetWork;
import com.jjwxc.jwjskandriod.framework.http.FFNetWorkCallBack;
import com.jjwxc.jwjskandriod.framework.imageloader.FFImageLoader;
import com.jjwxc.jwjskandriod.framework.utils.FFUtils;
import com.jjwxc.jwjskandriod.framework.utils.PreUtils;
import com.jjwxc.jwjskandriod.model.ReportResponse;
import com.jjwxc.jwjskandriod.readActivity.ReadActivity;
import com.jjwxc.jwjskandriod.readActivity.bean.CollBookBean;
import com.jjwxc.jwjskandriod.readActivity.local.ReadSettingManager;
import com.jjwxc.jwjskandriod.readActivity.view.TxtChapter;

import java.util.List;

public class ReadMorePop {

    public void readMoreWindow(View v, Activity context, CollBookBean bean, List<TxtChapter> mChapterList, int mChapterPos,ReadMoreListener readMoreListener) {


        View view = LayoutInflater.from(context).inflate(R.layout.read_more_pop, null, false);

        ImageView img_book = view.findViewById(R.id.img_book);
        TextView tv_name = view.findViewById(R.id.tv_name);
        TextView tv_info = view.findViewById(R.id.tv_info);
        TextView tv_des = view.findViewById(R.id.tv_des);
        Switch sh_swit = view.findViewById(R.id.sh_swit);

        LinearLayout ll_say = view.findViewById(R.id.ll_say);
        LinearLayout ll_jubao = view.findViewById(R.id.ll_jubao);
        LinearLayout ll_moredown = view.findViewById(R.id.ll_moredown);
        ConstraintLayout ll_read_more = view.findViewById(R.id.ll_read_more);


        FFImageLoader.loadImage(context, img_book, bean.getCover(), bean.get_id(), true);
        tv_name.setText(bean.getTitle());
        tv_info.setText(bean.getAuthor());
        //设置开关
        sh_swit.setChecked(PreUtils.getBoolean(mChapterList.get(0).getBookId(), false));

        //1.构造一个PopupWindow，参数依次是加载的 View，宽高
        final PopupWindow popWindow = new PopupWindow(view,
                FFUtils.getPx(230), FFUtils.getPx(248), true);

        popWindow.setAnimationStyle(R.anim.anim_pop);  //设置加载动画

        //这些为了点击非PopupWindow区域，PopupWindow会消失的，如果没有下面的
        //代码的话，你会发现，当你把PopupWindow显示出来了，无论你按多少次后退键
        //PopupWindow并不会关闭，而且退不出程序，加上下述代码可以解决这个问题

        popWindow.setTouchable(true);
        popWindow.setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return false;
                // 这里如果返回true的话，touch事件将被拦截
                // 拦截后 PopupWindow的onTouchEvent不被调用，这样点击外部区域无法dismiss
            }
        });

        popWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));    //要为popWindow设置一个背景才有效

        //设置 popupWindow 显示的位置，参数依次是参照 View，x轴的偏移量，y轴的偏移量
        popWindow.showAsDropDown(v, 20, v.getPaddingBottom());

        ReadSettingManager mSettingManager = ReadSettingManager.getInstance();
        if (mSettingManager.isNightMode()) {
            ll_read_more.setBackgroundResource(R.drawable.read_more_icon_black);
        } else {
            ll_read_more.setBackgroundResource(R.drawable.read_more_icon);
        }
        //设置 popupWindow 里的按钮的事件
        ll_moredown.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View vs) {
                if(PreUtils.getBoolean("tourist", false)){
                    FFApplication.showToast("游客模式下无法使用");
                    return;
                }
                new DownMorePop().downMorePopWindow(v, context, mChapterList, mChapterPos,bean.isMonthSubscribe(), new DownMorePop.DownMoreListener() {
                    @Override
                    public void onItemClick(int pos) {
                        if (pos == 1) {
                            ((ReadActivity) context).refreshBookCategory(true);
                        }
                    }
                });
            }
        });
        sh_swit.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(PreUtils.getBoolean("tourist", false)){
                    FFApplication.showToast("游客模式下无法使用");
                    return;
                }
                PreUtils.setBoolean(mChapterList.get(0).getBookId(), isChecked);
                readMoreListener.onValueClick("swit");
            }
        });

        tv_des.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, BookDetailsActivity.class);
                intent.putExtra("bookId", bean.get_id());
                context.startActivity(intent);
            }
        });
        ll_say.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(PreUtils.getBoolean("tourist", false)){
                    FFApplication.showToast("游客模式下无法使用");
                    return;
                }
                if (!PreUtils.getBoolean("say_body", false)) {
                    FFApplication.showToast("作者有话说已打开");
                    PreUtils.setBoolean("say_body", true);
                } else {
                    FFApplication.showToast("作者有话说已关闭");
                    PreUtils.setBoolean("say_body", false);
                }
            }
        });

        ll_jubao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new FFNetWork().post(Url.report, null, new FFNetWorkCallBack<ReportResponse>() {
                    @Override
                    public void onSuccess(ReportResponse response) {
                        if (response.getCode() == 200) {
                            Intent intent = new Intent(context, WebActivity.class);
                            intent.putExtra("WEB_URL", response.getData());
                            intent.putExtra("WEB_NAME", "举报");
                            context.startActivity(intent);
                        }

                    }

                    @Override
                    public boolean onFail(ReportResponse response) {
                        return false;
                    }
                }, ReportResponse.class,"{\"bookId\":\""+mChapterList.get(0).getBookId()+"\",\"reportType\":\"sexy\"}");
            }
        });
    }

    public interface ReadMoreListener {
        void onValueClick(String value);
    }
}
